<template>
	<view class="qrCode_box">
		<bottom-rect type="normal"></bottom-rect>
		<!-- 卡片内容 -->
		<view class="card_container">
			<!-- 二维码 -->
			<u-qrcode class="qrCode" ref="qrcode" canvas-id="qrcode" :size="qrcode_size" :value="qrCode_value"
				:options="options"></u-qrcode>
			<view class="time_box">
				<!-- 生成时间 -->
				<text class="time">生成时间：{{utils.myDate(create_time, 1)}}</text>
				<!-- 刷新 -->
				<image @click="qrcode_remake" :class="is_refresh?'refresh_active':''"
					src="/subpkg/qrCodePage/static/reLoad.png"></image>
			</view>

			<!-- 下方用户具体信息，用于装饰 -->
			<view class="userInfo">
				<!-- 头像 -->
				<view class="avatar">
					<image
						:src="userInfo.avatarUrl"
						mode="aspectFill"></image>
				</view>
				<!-- 昵称和真实姓名 -->
				<view class="name_box">
					<text class="nickname font-weight-bold main-font-color">{{userInfo.nickname}}</text>
					<text class="name">{{userInfo.profile.name}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script module="utils" src="@/wxcomponents/wxs/utils.wxs" lang="wxs"></script>
<script>
	import bottomRect from '@/components/bottomRect/bottomRect.vue'

	const app = getApp()

	export default {
		name: 'qrCode',
		components: {
			bottomRect
		},
		data() {
			return {
				// 是否刷新
				is_refresh: false,
				// 用户信息
				userInfo:{},
				// 时间的_id
				_id: '',
				// 二维码的值
				qrCode_info: {},
				// 二维码大小（单位rpx）
				create_time: 0,
				qrsize: 440,
				options: {
					margin: uni.upx2px(30),
					darkBlock: {},
					foreground: {
						color: '#014da1'
					}
				}
			}
		},
		computed: {
			// 二维码大小的单位转换
			qrcode_size() {
				return uni.upx2px(this.qrsize)
			},
			// 二维码的值
			qrCode_value() {
				this.qrCode_info._id = this._id
				this.qrCode_info.create_time = this.create_time
				this.qrCode_info.UID = app.globalData.userInfo.profile.student_ID
				const string = JSON.stringify(this.qrCode_info)
				return string
			}
		},
		methods: {
			// 刷新二维码
			qrcode_remake() {
				this.is_refresh = true
				setTimeout(() => {
					this.is_refresh = false
				}, 2000)
				this.create_time = new Date().getTime()
				this.qrCode_info.create_time = this.create_time
				this.$refs.qrcode.remake()
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '加载中...'
			})
			this._id = options._id
			this.create_time = new Date().getTime()
			this.userInfo = app.globalData.userInfo
			setTimeout(() => {
				uni.hideLoading()
			}, 300)
		},
	}
</script>

<style scoped lang="scss">
	.qrCode_box {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #F7F7F7;
		width: 100vw;
		height: 100vh;

		.card_container {
			width: 616rpx;
			height: 834rpx;
			border-radius: 20rpx;
			background-color: white;
			box-shadow: 0 0 8px #b8b8b8;
			margin-top: -90rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			overflow: hidden;

			.qrCode {
				box-shadow: 0 0 8px #b8b8b8;
				margin-top: 20rpx;
			}

			.time_box {
				display: flex;
				align-items: center;
				margin: 20rpx 0 30rpx 10rpx;

				.time {
					color: #B3B3B3;
					font-size: 28rpx;
					margin-right: 15rpx;
				}

				image {
					margin-top: -4rpx;
					width: 45rpx;
					height: 45rpx;
				}

				.refresh_active {
					transform: rotate(1080deg);
					transition: transform 2s;
				}
			}


			.userInfo {
				border-top: 2rpx solid #EFEFEF;
				width: 520rpx;
				height: 200rpx;
				display: flex;
				align-items: center;

				.avatar {
					width: 120rpx;
					height: 120rpx;
					border-radius: 200rpx;
					overflow: hidden;
					margin-right: 20rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.name_box {
					display: flex;
					flex-direction: column;

					.nickname {
						font-size: 40rpx
					}

					.name {
						font-size: 28rpx;
						color: #B3B3B3;
					}
				}
			}
		}
	}
</style>
